﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace MyLibary.DAL
{
    class DAL_LoaiSanPham
    {
        internal static DAL_Connection dal = new DAL_Connection();
        /// <summary>
        /// lấy danh sách tất cả loại sản phẩm
        /// </summary>
        /// <returns></returns>
        internal static List<BIZ.BIZ_LoaiSanPham> GetLoaiSanPhams()
        {
            DataTable tb= dal.ExecuteSelect("SELECT * FROM LoaiSanPham");
            List<BIZ.BIZ_LoaiSanPham> result = new List<BIZ.BIZ_LoaiSanPham>();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                result.Add(new BIZ.BIZ_LoaiSanPham(tb.Rows[i][0].ToString(), tb.Rows[i][1].ToString()));
            }
            return result;
        }
        /// <summary>
        /// lấy 1 loại sản phẩm theo mã
        /// </summary>
        /// <param name="maSanPham"></param>
        /// <returns></returns>
        internal static BIZ.BIZ_LoaiSanPham GetLoaiSanPham(string maSanPham)
        {
            DataTable dt = dal.ExecuteSelect("SELECT * FROM LoaiSanPham WHERE MaLoai='" + maSanPham + "'");
            if (dt.Rows.Count < 1)
                return null;
            return new BIZ.BIZ_LoaiSanPham(dt.Rows[0][0].ToString(),dt.Rows[0][1].ToString());
        }
        /// <summary>
        /// xóa 1 loại sản phẩm
        /// </summary>
        /// <param name="LoaiSanPham"></param>
        /// <returns></returns>
        internal static bool Delete(BIZ.BIZ_LoaiSanPham LoaiSanPham)
        {
            return dal.ExecuteUpdate("DELETE LoaiSanPham WHERE MaLoai='" + LoaiSanPham.MaLoai + "'");
        }
        /// <summary>
        /// update 1 loại sản phẩm
        /// </summary>
        /// <param name="LoaiSanPham"></param>
        /// <returns></returns>
        internal static bool Update(BIZ.BIZ_LoaiSanPham LoaiSanPham)
        {
            return dal.ExecuteUpdate("UPDATE LoaiSanPham SET MaLoai='" + LoaiSanPham.MaLoai + "',TenLoai='" + LoaiSanPham.TenLoai + "'");
        }
        /// <summary>
        /// insert 1 loại sản phẩm
        /// </summary>
        /// <param name="LoaiSanPham"></param>
        /// <returns></returns>
        internal static bool Insert(BIZ.BIZ_LoaiSanPham LoaiSanPham)
        {
            return dal.ExecuteUpdate("INSERT INTO LoaiSanPham  VALUES('"+LoaiSanPham.MaLoai+"','"+LoaiSanPham.TenLoai+"')");
        }
        
    }
}
